package com.ruoyi.project.system.course.service;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.monitor.server.domain.Sys;
import com.ruoyi.project.system.course.domain.CourseArrange;
import com.ruoyi.project.system.course.domain.CourseClass;
import com.ruoyi.project.system.course.domain.CourseTeacher;
import com.ruoyi.project.system.course.mapper.CourseArrangeMapper;
import com.ruoyi.project.system.course.mapper.CourseClassMapper;
import com.ruoyi.project.system.course.mapper.CourseTeacherMapper;
import com.ruoyi.project.system.dept.controller.DeptController;
import com.ruoyi.project.system.dept.mapper.DeptMapper;
import com.ruoyi.project.system.dept.service.DeptServiceImpl;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.mapper.PostMapper;
import com.ruoyi.project.system.user.mapper.UserMapper;
import com.ruoyi.project.system.user.mapper.UserPostMapper;
import com.ruoyi.project.system.user.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.course.mapper.CourseMapper;
import com.ruoyi.project.system.course.domain.Course;
import com.ruoyi.project.system.course.service.ICourseService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程 服务层实现
 * 
 * @author Waynnn
 * @date 2019-03-21
 */
@Service
public class CourseServiceImpl implements ICourseService 
{
	@Autowired
	private CourseMapper courseMapper;

	@Autowired
	private CourseClassMapper courseClassMapper;

	@Autowired
	private CourseTeacherMapper courseTeacherMapper;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private DeptMapper deptMapper;

	@Autowired
	private PostMapper postMapper;

	@Autowired
	private UserPostMapper userPostMapper;

	@Autowired
	private DeptServiceImpl deptService;

	@Autowired
	private UserServiceImpl userService;

	@Autowired
	private CourseArrangeMapper courseArrangeMapper;
	/**
     * 查询课程信息
     * 
     * @param courseId 课程ID
     * @return 课程信息
     */
    @Override
	public Course selectCourseById(Long courseId)
	{
	    return courseMapper.selectCourseById(courseId);
	}


    /**
     * 查询课程列表
     * 
     * @param course 课程信息
     * @return 课程集合
     */
	@Override
	public List<Course> selectCourseList(Course course)
	{
		List<Course> courses=courseMapper.selectCourseList(course);
		for (Course course1:courses){
			if (course1.getCourseId()!=null){
				course1.setTeacherNames(userService.selectTeacherByCourseIdsetName(course1.getCourseId()));
				course1.setClassNames(deptService.selectClassesByCourseIdsetName(course1.getCourseId()));
			}

		}
	    return courses;
	}
	
    /**
     * 新增课程
     * 
     * @param course 课程信息
     * @return 结果
     */
	@Override
	@Transactional
	public int insertCourse(Course course)
	{

		//新增课程信息
		course.setCreateBy(ShiroUtils.getLoginName());
		int rows= courseMapper.insertCourse(course);
		//新增课程班级关联
		insertCourseClass(course);
		//新增课程老师关联
		insertCourseTeacher(course);

		insertCourseArrangeEmpty(course);
	    return rows;
	}

	@Override
	public void insertCourseArrangeEmpty(Course course){
		Long[] classes=course.getClassIds();

		if (StringUtils.isNotNull(classes))
		{
			// 新增用户与角色管理
			List<CourseArrange> list = new ArrayList<>();
			for (Long classId : course.getClassIds())
			{
				CourseArrange courseArrange=new CourseArrange();
				courseArrange.setDeptId(classId);
				courseArrange.setCourseId(course.getCourseId());
				courseArrange.setTeacherId(null);
				courseArrange.setWeekDay("未安排");
				courseArrange.setStartTime("未安排");
				courseArrange.setEndTime("未安排");
				courseArrange.setPlace("未安排");
				courseArrange.setCreateBy(ShiroUtils.getLoginName());
				list.add(courseArrange);
			}
			if (list.size() > 0)
			{
				courseArrangeMapper.insertCourseArranges(list);
			}
		}
	}

	/**
	 * 新增课程班级关联
	 *
	 * @param course 课程信息
	 *
	 */
	@Override
	public void insertCourseClass(Course course) {
		Long[] classes=course.getClassIds();

		if (StringUtils.isNotNull(classes))
		{

			List<CourseClass> list = new ArrayList<CourseClass>();
			for (Long classId : course.getClassIds())
			{
				CourseClass cc = new CourseClass();
				cc.setCourseId(course.getCourseId());
				cc.setDeptId(classId);
				list.add(cc);
			}
			if (list.size() > 0)
			{
				courseClassMapper.batchCourseClass(list);
			}
		}
	}

	/**
	 * 新增课程老师关联
	 *
	 * @param course 课程信息
	 */
	@Override
	public void insertCourseTeacher(Course course) {
		Long[] teachers=course.getTeacherIds();

		if (StringUtils.isNotNull(teachers))
		{
			// 新增用户与角色管理
			List<CourseTeacher> list = new ArrayList<CourseTeacher>();
			for (Long teacherId : course.getTeacherIds())
			{
				CourseTeacher ct = new CourseTeacher();
				ct.setCourseId(course.getCourseId());
				ct.setUserId(teacherId);
				list.add(ct);
			}
			if (list.size() > 0)
			{
				courseTeacherMapper.batchCourseTeacher(list);
			}
		}
	}


	/**
     * 修改课程
     * 
     * @param course 课程信息
     * @return 结果
     */
	@Override
	@Transactional
	public int updateCourse(Course course)
	{

		Long courseId=course.getCourseId();
		course.setUpdateBy(ShiroUtils.getLoginName());
		courseClassMapper.deleteCourseClassById(courseId);
		insertCourseClass(course);
		courseTeacherMapper.deleteCourseTeacherById(courseId);
		insertCourseTeacher(course);
		courseArrangeMapper.deleteCourseArrangeByCourseId(courseId);
		insertCourseArrangeEmpty(course);
	    return courseMapper.updateCourse(course);
	}

	/**
     * 删除课程对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteCourseByIds(String ids) throws Exception
	{
		Long[] courseIds=Convert.toLongArray(ids);
		for (Long courseId:courseIds){
			Course course=selectCourseById(courseId);
			if (courseClassMapper.countCourseClassByCourseId(courseId)>0){
				throw new BusinessException(String.format("%1$s尚有班级开设,不能删除", course.getCourseName()));
			}
			if (courseTeacherMapper.countCourseTeacherByCourseId(courseId)>0){
				throw new BusinessException(String.format("%1$s尚有教师任教,不能删除", course.getCourseName()));
			}

		}
		return courseMapper.deleteCourseByIds(Convert.toStrArray(ids));
	}

	/**
	 * 查询课程任教老师
	 * @param  curseId 课程ID
	 * @return
	 */
	@Override
	public String selectTeacherCourseGroup(Long curseId) {

		return null;
	}

	@Override
	public String selectClassCourseGroup(Long curseId) {
		return null;
	}

	@Override
	public int changeStatus(Course course) {
		return courseMapper.updateCourse(course);
	}

}
